package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.reggie.common.BaseContext;
import com.example.reggie.common.R;
import com.example.reggie.entity.User;
import com.example.reggie.service.impl.UserServiceImpl;
import com.example.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserServiceImpl userService;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 发送手机短信验证码到redis
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user){
        String phone = user.getPhone();
        log.info("手机号码为{}获取验证码",phone);
        String key=null;
        if (phone!=null){
            //生成验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("{}的验证码为:{}",phone,code);
            key="logincode:"+phone;
            //key: "logincode:13712018079"
            redisTemplate.opsForValue().set(key,code);
            //设置TTL为两分钟
            Boolean expire = redisTemplate.expire(key,120,TimeUnit.SECONDS);
            log.info("设置验证码ttl成功与否{}",expire);
            Long expire1 = redisTemplate.getExpire(key);
            log.info("过期时间:{}",expire1);
        }
        //重新获取不为空,发送成功
        if (null==redisTemplate.opsForValue().get(key)){
            log.info("发送失败");
            return R.error("发送失败,请重试");
        }
        log.info("发送成功");
        return R.success("发送成功");
    }

    /**
     * 移动端用户登录
     * @param map
     * @param
     * @return
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpServletRequest request){
        //获取手机号
        String phone = map.get("phone").toString();
        //获取验证码
        String code = map.get("code").toString();
        //判断验证码
        String code1 =(String) redisTemplate.opsForValue().get("logincode:" + phone);
        if (code1==null||!code1.equals(code)){
            return R.error("登陆失败");
        }
        //删除缓存
        redisTemplate.delete("logincode:" + phone);

        LambdaQueryWrapper<User>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone,phone);
        User user = userService.getOne(queryWrapper);
        //新用户
        if (user==null){
            user=new User();
            user.setPhone(phone);
            user.setStatus(1);
            userService.save(user);
        }
        //回显id
        log.info("id:{}",user.getId());
        request.getSession().setAttribute("user",user.getId());
//        BaseContext.setCurrentId(user.getId());
        return R.success(user);
    }

    /**
     * 员工退出
     * @param request
     */
    @PostMapping("/loginout")
    public R<String> logout(HttpServletRequest request){
        // 1.清理session
        request.getSession().removeAttribute("user");
        // 2.退出
        return R.success("退出成功");
    }


}
